package com.bjpower.controller;

import com.bjpower.models.Result;
import com.bjpower.models.dto.OperatorLoginDto;
import com.bjpower.models.meuns.SubMenu;
import com.bjpower.models.request.OperatorLoginRequest;
import com.bjpower.services.OperatorLoginService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理登录请求
 * @CorssOrigin解决跨域问题
 */
@CrossOrigin
@RestController
public class OperatorController {

    @Resource
    private OperatorLoginService operatorLoginService;


    /**
     * 处理登录请求验证登录用户是否存在并返回用户信息以及jwt
     * 添加@Validated注解对请求参数进行校验，可以加入自定义的验证规则，此注解只需要加入一次就会对全局的请求参数进行校验
     * @Validated注解会自动校验请求参数，如果参数不符合要求，则会抛出异常
     * @param operatorLoginRequest
     * @return
     */
    @PostMapping("/operator/login")
    public Result login(@RequestBody @Validated OperatorLoginRequest operatorLoginRequest) {
        OperatorLoginDto operatorLoginDto = operatorLoginService.loginService(operatorLoginRequest);
        return Result.ok(operatorLoginDto);
    }

    /**
     * 获取用户的菜单选项
     * @return
     */
    @GetMapping("/operator/menus")
    public Result getMenus(@RequestHeader("operatorId") Long operatorId){
        //根据用户id查询用户菜单
        List<SubMenu> subMenuList = operatorLoginService.getMenusByOperatorId(operatorId);

        return Result.ok(subMenuList);
    }
}
